package cn.waka.sys.admin.controller;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Merchant;
import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.object.Result;
import cn.waka.sys.admin.service.MerchantService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/merchant")
public class MerchantController extends BaseController{

	
	@Autowired
	private MerchantService merchantService;
	
	@GetMapping(path = "/index{type}",name="商家分页")
	public ModelAndView pageList3(@PathVariable("type")int type,HttpServletRequest req, HttpServletResponse res){
		String pStr=req.getParameter("page");
		Page p=new Page();
		if(pStr!=null&&Validator.isNum(pStr)){
			p.setPage(Integer.parseInt(pStr));
		}
		String sizeStr=req.getParameter("size");
		if(sizeStr!=null&&Validator.isNum(sizeStr)){
			p.setSize(Integer.parseInt(sizeStr));
		}
		
		ModelAndView mv=new ModelAndView("/merchant/index");
		
		merchantService.findPage(p,type);
		p.addParam("type", type);
		mv.addObject("page", p);
		if(type==1){
			mv.addObject("title", "运营商管理");
		}else if(type==2){
			mv.addObject("title", "供应商管理");
		}else if(type==3){
			mv.addObject("title", "内容提供商管理");
		}
		mv.addObject("type",type);
		return mv;
		
	}
	
	
	@RequestMapping(path = "/add{type}", name="添加商家")
	public ModelAndView add1(@PathVariable("type")int type,HttpServletRequest req, HttpServletResponse res){
		Merchant obj=new Merchant();
		ModelAndView mv=new ModelAndView("/merchant/edit");
		obj.setType(type);
		mv.addObject("vo", obj);
		mv.addObject("type", type);
		return mv;
	}
	
	@RequestMapping(path = "/edit", name="编辑商家")
	public ModelAndView edit(@RequestParam(name="id",required=false)String idStr,HttpServletRequest req, HttpServletResponse res){
		
		int id=0;
		if(idStr!=null&&Validator.isNum(idStr)){
			id=Integer.parseInt(idStr);
		}
		Merchant obj=null;
		if(id==0){
			obj=new Merchant();
		}else{
			obj=merchantService.findById(id);
			if(obj==null){
				obj=new Merchant();
			}
		}
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("vo", obj);
		return mv;
	}
	
	
	
	
	@RequestMapping(path = "/save{type}", name="保存商家")
	@ResponseBody
	public Result save(@PathVariable("type")int type,@RequestParam(name="id",required=false)String idStr,@RequestParam(name="name",required=false)String name,@RequestParam(name="desc",required=false)String desc,HttpServletRequest req, HttpServletResponse res, HttpSession sess){
		int id=0;
		if(idStr!=null&&Validator.isNum(idStr)){
			id=Integer.parseInt(idStr);
		}
		if(name==null){
			return error(0,"名称不能为空！");
		}
		
		Merchant m=new Merchant();
		m.setId(id);
		m.setDesc(desc);
		m.setCreateTime(new Timestamp(System.currentTimeMillis()));
		m.setName(name);
		m.setType(type);
		
		
		int rs = merchantService.save(m);
		
		if(rs>0){
			return error(1,"保存成功！");
		}else{
			return error(0,"保存失败！");
		}
	}
	
}
